<?php
//勿删  微信支付真实案例(FLowerHua)
class Member_chongzhiController extends Member {
    public $member_chongzhi;
	public $member;
	public $chongzhi;
	
	
    public function __construct() {
		parent::__construct();
		$this->member_chongzhi = $this->model('member_chongzhi');
		$this->member = $this->model('member');
		$this->chongzhi = $this->model('chongzhi');
		
		if(!get_cookie('openid')){
		  $this->oauthlogin();//微信授权跟获取
		}
		
	}
	
	public function indexAction() {

		$member_id = get_cookie('yuangong_id');//判断是否已经登录
		if(empty($member_id)){
			$back = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : url('member/');
			$this->redirect(url('member/', array('back' => urlencode($back))));
		}
		$type = get_cookie('type');
		if($type){
			$this->redirect(url('member/'));
		}
		$member = $this->db->where('id',$member_id)->get('member')->row_array();
		
		$where = '1=1 and member_id='.$member_id;
		$page = (int)$this->get('page') ? (int)$this->get('page') : 1;
	    //$pagesize = isset($this->site['SITE_ADMIN_PAGESIZE']) && $this->site['SITE_ADMIN_PAGESIZE'] ? $this->site['SITE_ADMIN_PAGESIZE'] : 8;
		$pagesize = 20;
	    $urlparam = array('page' => '{page}');
		$order = 'id desc';
		$limit = ($page-1)*$pagesize.",$pagesize";
		
		$data =  $this->member->get_chongzhi_list($where,$order ,$limit); //列表
		//print_r($data);
		$total = $this->member->get_chongzhi_list($where,$order ,$limit,true); //统计数量	

		$count = array();	//统计各个状态的数据量
	    $pagelist = $this->instance('pagelist');	//加载分页类
		$pagelist->loadconfig();
		$pagelist = $pagelist->total($total)->url(url('member/member_chongzhi/', $urlparam))->num($pagesize)->page($page)->output();
		$backurl = $this->get('back') ? $this->get('back') : (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : url('member/'));
		$pagenum = ceil($total/$pagesize);
		
		$this->view->assign(array(
			'indexc' => 'chongzhi',
			'member' => $member,
			'pagenum' => $pagenum,
			'meta_title' => lang('充值'),
	        'page' => $page,
	        'list' => $data,
			'total' => $total,
	        'pagelist' => $pagelist
	    ));
		$this->view->display('member/member_chongzhi');
	}

	
	//填写充值金额
	public function addAction(){
		$member_id = get_cookie('yuangong_id');//判断是否已经登录
		if(empty($member_id)){
			$back = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : url('member/');
			$this->redirect(url('member/', array('back' => urlencode($back))));
		}
		$type = get_cookie('type');
		if($type){
			$this->redirect(url('member/'));
		}
		if($this->post('submit')){
			$jine = $this->post('jine');
			$sn = "sn".randomkeys(15);
			$data['member_id'] = $member_id;
			$data['jine'] = $jine;
			$data['sn'] = $sn;
			$data['paystatus'] = 1;
			$data['addtime'] = time();
			$flg = $this->member_chongzhi->set(0,$data);
			if($flg){
				$cid = $this->chongzhi->get_chonghzi_time($data['addtime']);
				$this->redirect(url('member/member_chongzhi/pay', array('id' => $cid)));
				exit;
			}
		}
		$this->view->assign(array(
			'indexc' => 'chongzhi',
			'meta_title' => lang('充值金额'),
	    ));
		$this->view->display('member/chongzhi_shuliang');
	}
	
	
	//确认支付
	public function payAction(){
		$member_id = get_cookie('yuangong_id');//判断是否已经登录
		if(empty($member_id)){
			$back = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : url('member/');
			$this->redirect(url('member/', array('back' => urlencode($back))));
		}
		$type = get_cookie('type');
		if($type){
			$this->redirect(url('member/'));
		}
		
		$id = (int)$this->get('id');
		$list = $this->chongzhi->get_one_list($id);
		//微信支付
		require_once "wxpay/lib/WxPay.Api.php";
		require_once "wxpay/example/WxPay.JsApiPay.php";
		require_once 'wxpay/example/log.php';
		
		//初始化日志
		$logHandler= new CLogFileHandler("wxpay/logs/".date('Y-m-d').'.log');
		$log = Log::Init($logHandler, 15);
		
		//①、获取用户openid
		$tools = new JsApiPay();
		$openId = $list['m_openid'];
		
		//②、统一下单
		$input = new WxPayUnifiedOrder();
		$input->SetBody("充值");
		$input->SetAttach("充值单号：".$list['sn']);
		$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));//订单号
		$input->SetTotal_fee($list['jine']*100);//金额
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		$input->SetGoods_tag("会员充值");
		$input->SetNotify_url("http://FlowerHua.com/wxpay/example/notify.php?sn=".$list['sn']);
		$input->SetTrade_type("JSAPI");
		$input->SetOpenid($openId);
		$order = WxPayApi::unifiedOrder($input);
		$jsApiParameters = $tools->GetJsApiParameters($order);
		$this->view->assign('jsApiParameters',$jsApiParameters);
	
		//
		//$this->redirect(url('member/member_chongzhi'));
		//exit;
		$this->view->assign(array(
			'list' => $list,
			'indexc' => 'chongzhi',
			'meta_title' => lang('微信支付'),
	    ));
		$this->view->display('member/chongzhi_zhifu');
	}
	
	
	//支付结果，客户端回调
	public function payokAction(){
		$id = (int)$this->get('id');//客户端回调
		if($id){
			$list = $this->chongzhi->get_jinm_one($id);
			if($list['paystatus'] == 2){
				$this->redirect(url('member/member_chongzhi'));	
				exit;
			}
			$this->member->upchongzhijine($list['member_id'],$list['jine']);//修改充值金额，个人总金额，余额
			$data['paystatus'] = 2;
			$this->member_chongzhi->set($list['id'],$data);
			$this->redirect(url('member/member_chongzhi'));
			exit;
		}
	}

	//支付结果，服务器回调
	public function payok_fuwuAction(){
		$sn = $this->get('sn');
		if($sn){
		  $list = $this->chongzhi->get_xml_one($sn);
		  $doc = new DOMDocument();
		  $doc->load('wxpay/example/logxml/'.$sn.'.xml');
		  $books = $doc->getElementsByTagName("xml");
		  foreach($books as $book){
			$authors = $book->getElementsByTagName("return_code");
			$author = $authors->item(0)->nodeValue; 
		  }
		  if($author == 'SUCCESS'){//支付成功
		  	if($list['paystatus'] == 2){
				exit;
			}
			$this->member->upchongzhijine($list['member_id'],$list['jine']);//修改充值金额，个人总金额，余额
			$data['paystatus'] = 2;
			$this->member_chongzhi->set($list['id'],$data);
			exit;
		  }
		}
	}
	
}